import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:verse_systems/pages/certification/certification_control.dart';
import 'package:verse_systems/res/colors.dart';
import 'package:verse_systems/widgets/custom_loan_amount_appbar.dart';
import 'package:verse_systems/widgets/sp_button.dart';
import 'package:card_swiper/card_swiper.dart';

class CertificationConfirmAmount extends StatefulWidget {
  const CertificationConfirmAmount({super.key});

  @override
  State<CertificationConfirmAmount> createState() =>
      _CertificationConfirmAmountState();
}

class _CertificationConfirmAmountState
    extends State<CertificationConfirmAmount> {
  final control = Get.find<CertificationControl>();
  final state = Get.find<CertificationControl>().state;



  @override
  Widget build(BuildContext context) {
    return GetBuilder<CertificationControl>(
        init: control,
        builder: (_) {
          return SingleChildScrollView(
              child: Container(
            color: Colours.system_background_color,
            child: Column(
              children: [
                CustomLoanAmountAppbar(
                  amountType: LoantType.loanhalf,
                  amount: state.amount_value,
                  time: state.time_value,
                ),
                _confirmAmount,
                Container(
                  margin: EdgeInsets.fromLTRB(30.w, 21.w, 30.w, 23),
                  child: Text(
                    state.confirm_quota_description,
                    style: TextStyle(
                      color: Colours.app_forward_color,
                      fontSize: 10.sp,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
                Container(
                  margin: EdgeInsets.fromLTRB(0, 35.w, 0, 46.w),
                  width: 310.w,
                  child: SPButton(
                    minHeight: 50.w,
                    fontSize: 18.sp,
                    text: "apply_loan".tr,
                    textColor: Colours.app_main_color,
                    backgroundColor: Colours.app_main_background_color,
                    radius: 25.w,
                    onPressed: control.onConfirmAmountToDetails,
                  ),
                ),
              ],
            ),
          ));
        });
  }

  Widget get _amountBorrowed {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        width: MediaQuery.of(context).size.width,
        color: Colours.app_main_color,
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "amount_borrowed".tr,
                  style: TextStyle(
                    color: Colours.app_forward_color,
                    fontSize: 16.sp,
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  height: 44.w,
                  width: 100.w,
                  child: SwiperDiy(
                    swiperList: state.product,
                    onIndexChanged: control.onIndexChangedAmount,
                  ),
                )
              ],
            ),
            Container(
              height: 1.w,
              color: Colours.app_text_line_color,
            ),
          ],
        ));
  }

  Widget get _amountDay {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        width: MediaQuery.of(context).size.width,
        color: Colours.app_main_color,
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "term_loan".tr,
                  style: TextStyle(
                    color: Colours.app_forward_color,
                    fontSize: 16.sp,
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  height: 44.w,
                  width: 100.w,
                  child: SwiperDay(
                    swiperList: state.product_day,
                    onIndexChanged: control.onIndexChangeddayInfo,
                  ),
                )
              ],
            ),
            Container(
              height: 1.w,
              color: Colours.app_text_line_color,
            ),
          ],
        ));
  }

  Widget get _confirmAmount {
    final amount = Get.arguments["amount"];
    final day = Get.arguments["day"];
    if (amount == null && day == null) {
      return Column(
        children: [
          _amountBorrowed,
          _amountDay,
          SizedBox(height: 11.w),
          Column(children: _confirmAmountList)
        ],
      );
    }
    return Column(children: _halfAmountList);
  }

  List<Widget> get _halfAmountList {
    return state.confirmAmount.map((val) {
      return Container(
          padding: EdgeInsets.symmetric(horizontal: 24.w),
          width: MediaQuery.of(context).size.width,
          color: Colours.app_main_color,
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    val["key"].toString(),
                    style: TextStyle(
                      color: Colours.app_forward_color,
                      fontSize: 16.sp,
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    height: 44.w,
                    child: Text(
                      val["value"].toString(),
                      style: TextStyle(
                        color: Colours.app_line_color,
                        fontSize: 15.sp,
                      ),
                    ),
                  )
                ],
              ),
              Container(
                height: 1.w,
                color: Colours.app_text_line_color,
              ),
            ],
          ));
    }).toList();
  }

  List<Widget> get _confirmAmountList {
    return state.confirmAmount.map((val) {
      return Container(
          padding: EdgeInsets.symmetric(horizontal: 24.w),
          width: MediaQuery.of(context).size.width,
          color: Colours.app_main_color,
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    val["key"].toString(),
                    style: TextStyle(
                      color: Colours.app_forward_color,
                      fontSize: 16.sp,
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    height: 44.w,
                    child: Text(
                      val["value"].toString(),
                      style: TextStyle(
                        color: Colours.app_line_color,
                        fontSize: 15.sp,
                      ),
                    ),
                  )
                ],
              ),
              Container(
                height: 1.w,
                color: Colours.app_text_line_color,
              ),
            ],
          ));
    }).toList();
  }
}

class SwiperDiy extends StatelessWidget {
  final List swiperList;
  final Function(int index) onIndexChanged;
  const SwiperDiy(
      {super.key, required this.swiperList, required this.onIndexChanged});
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 50.w,
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          // 配置图片地址
          return Center(
              child: Text(
            swiperList[index]["amount"]["value"].toString(),
            style: TextStyle(
              color: Colours.app_forward_color,
              fontSize: 16.sp,
            ),
          ));
        },
        // 配置图片数量
        itemCount: swiperList.length,
        onIndexChanged: onIndexChanged,
        // 底部分页器
        // pagination:  SwiperPagination(),
        // 左右箭头
        control: SwiperControl(color: Colours.app_main_text_color, size: 20.w),
        // 无限循环
        loop: false,
        // 自动轮播
        autoplay: false,
      ),
    );
  }
}

class SwiperDay extends StatelessWidget {
  final List swiperList;
  final Function(int index) onIndexChanged;
  const SwiperDay(
      {super.key, required this.swiperList, required this.onIndexChanged});
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 50.w,
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          // 配置图片地址
          return Center(
              child: Text(
            swiperList[index]["value"].toString(),
            style: TextStyle(
              color: Colours.app_forward_color,
              fontSize: 16.sp,
            ),
          ));
        },
        // 配置图片数量
        itemCount: swiperList.length,
        onIndexChanged: onIndexChanged,
        // 底部分页器
        // pagination:  SwiperPagination(),
        // 左右箭头
        control: SwiperControl(color: Colours.app_main_text_color, size: 20.w),
        // 无限循环
        loop: false,
        // 自动轮播
        autoplay: false,
      ),
    );
  }
}
